<?php
	include '../class/class.user.php';
	include '../class/class.kewenangan.php';

	session_start();
	if(isset($_REQUEST['login'])){
		$nip=$_POST['nip'];
		$pass=$_POST['password'];
		$error='';
		$msg=false;
		$dao=new UserDao();

		if(empty($nip) || empty($pass)){
			$error="Username dan Password Harus Diisi";
		}else {
			$user= new User();
			$user->nip=$nip;
			$user->password=$pass;

			if($dao->loginUser($user)){
				
				$user_detail=$dao->getUser($user);
				$cek=$dao->cekAktif($user_detail['id_user']);
				if($cek){
					$msg=true;
					$cekPass=$dao->cekPassword($nip);
					if($cekPass){
						$_SESSION['isDefault']=true;
					}else{
						$_SESSION['isDefault']=false;
					}
					$_SESSION['isLogged']=true;
					$_SESSION['start']=time();
					$_SESSION['expired']=$_SESSION['start']+ (60*60);
					$_SESSION['id_user']=$user_detail['id_user'];
					$_SESSION['nip']=$nip;
					$_SESSION['id_kewenangan']=$user_detail['id_kewenangan'];
				}else{
					$error="User Belum Aktif";
				}
			}else{
				$error="NIP atau Password Salah";
			}
		}
		$data=array(
			'msg'=> $msg,
			'error' => $error	
			);
		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_user_menu']) && $_SESSION['isLogged']==true){
		//session_start();
		$id_user=$_SESSION['id_user'];
		$id_kewenangan=$_SESSION['id_kewenangan'];

		$userDao=new UserDao();

		$nip=$userDao->getNip($id_user);

		$kewenanganDao= new KewenanganDao();

		$kewenangan=$kewenanganDao->getKewenangan($id_kewenangan);

		$data=array(
			'nip' => $nip,
			'kewenangan' => $kewenangan,
			'id_kewenangan' => $id_kewenangan,
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_isAdmin'])){
		$id_kewenangan=$_SESSION['id_kewenangan'];
		$msg=false;
		$id_kanwil=1;
		if($id_kewenangan=='1'){
			$msg=true;
		}

		$data=array(
			'msg' => $msg,
			'kanwil' => $id_kanwil,
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_Kewenangan'])){
		$id_kewenangan=$_SESSION['id_kewenangan'];
		$msg=false;
		if($id_kewenangan!='4'){
			$msg=true;
		}

		echo json_encode($msg);
	}

	if(isset($_REQUEST['aksi_menu'])){
		$id_kewenangan=$_SESSION['id_kewenangan'];
		$msg=false;
		if($id_kewenangan!='4'){
			$msg=true;
		}

		echo json_encode($msg);
	}

	if(isset($_REQUEST['aksi_default'])){
		$default=$_SESSION['isDefault'];
		if($default){
			$data=true;
		}

		echo json_encode($data);
	}


?>